
import os
import queue
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

queue = queue.Queue()
class FileChangeHandler(FileSystemEventHandler):
    def on_any_event(self, event):
        queue.put(event)


class SqlRunner():
    def __init__(self, parent=None):
        self.isBlock = False

    def begin(self):
        self.isBlock = False

    def wait(self):
        self.isBlock = True

    def run(self):
        while True:
            if queue.empty() != True and self.isBlock == False:
                print(queue.qsize())
                event = queue.get()
                if event.event_type == 'deleted' or event.event_type == 'created' or event.event_type == 'moved':
                    try:
                        if 'AppData\\Roaming\\Microsoft\\Windows\\Recent' not in event.src_path and 'filesearch\\index.db' not in event.src_path:
                            name = event.src_path.split('\\')[-1]
                            path = event.src_path.replace('\\%s' % name, '')
                            fileType = 0
                            if os.path.isfile(event.src_path) == True:
                                fileType = 1
                            suffix = ''
                            if '.' in event.src_path:
                                suffix = event.src_path.split('.')[-1]

                            hashvalue = hash('%s%s%s%d' %
                                             (name, path, suffix, fileType))
                            if event.event_type == 'deleted':
                                pass
                            elif event.event_type == 'created':
                                pass
                            else:  # moved                                
                                name_new = event.dest_path.split('\\')[-1]
                                path_new = event.dest_path.replace(
                                    '\\%s' % name_new, '')
                                suffix = ''
                                if '.' in event.dest_path:
                                    suffix = event.dest_path.split('.')[-1]

                                fileType = 0
                                if os.path.isfile(event.dest_path) == True:
                                    fileType = 1
                                hashvalue = hash('%s%s%s%d' %
                                                 (name_new, path_new, suffix, fileType))
                                pass
                    except BaseException as err:
                        print(err)

if __name__=='__main__':
    fch = FileChangeHandler()
    for d in ['d:\\','c:\\']:
        if os.path.isdir(d) == True:
            observer = Observer()
            observer.schedule(fch, d, recursive=True)
            observer.start()
    sr = SqlRunner()
    sr.run()